package com.project.web.wechat.controller.app.cpWeChat;

import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.Result;
import com.project.web.config.WxCorp.WxCpConfig;
import com.project.web.config.WxCorp.WxCpConfiguration;
import com.project.web.util.exception.WeChatCustomException;
import com.project.web.wechat.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpDepartmentService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 部门管理接口
 *
 * @author wyy
 * @date 2020-06-24
 */
@Api(tags = "部门管理接口")
@RestController("workWxDepartmentController")
@RequestMapping("/weChat/auth/workWx/department")
public class WorkWxDepartmentController extends BaseController {

    /**
     * 创建部门
     *
     * @return
     */
    @ApiOperation("创建部门")
    @PostMapping(value = "/create")
    public Result<Long> create(WxCpDepart wxCpDepart, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpDepartmentService departmentService = wxCpService.getDepartmentService();
        try {
            Long aLong = departmentService.create(wxCpDepart);
            return getResult(aLong);
        } catch (WxErrorException e) {
            log.error("创建部门出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 更新部门
     *
     * @return
     */
    @ApiOperation("更新部门")
    @PostMapping(value = "/update")
    public Result<WxCpDepart> update(WxCpDepart wxCpDepart, HttpServletRequest request) {

        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpDepartmentService departmentService = wxCpService.getDepartmentService();
        try {
            departmentService.update(wxCpDepart);
            return getResult(wxCpDepart);
        } catch (WxErrorException e) {
            log.error("更新部门出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 删除部门
     *
     * @return
     */
    @ApiOperation("删除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "departmentId", value = "部门ID", required = true, dataType = "Long")
    })
    @GetMapping(value = "/delete")
    public Result<WxCpUser> delete(Long departmentId, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpDepartmentService departmentService = wxCpService.getDepartmentService();
        try {
            departmentService.delete(departmentId);
            return getResult("success");
        } catch (WxErrorException e) {
            log.error("删除部门出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 获取部门列表
     *
     * @param departmentId 部门ID
     * @return
     */
    @ApiOperation("获取部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "departmentId", value = "部门ID", required = true, dataType = "Long")
    })
    @GetMapping(value = "/list")
    public Result<List<WxCpDepart>> list(Long departmentId, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpDepartmentService departmentService = wxCpService.getDepartmentService();
        try {
            List<WxCpDepart> list = departmentService.list(departmentId);
            return getResult(list);
        } catch (WxErrorException e) {
            log.error("获取部门列表出错：{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }
}
